/********************************************************/
/*	Copyright (C) 2016 Gong Li Bin		 	*/
/*	Project:	GoLib-1.0.0			*/
/*	Author:		gong_libin			*/
/*	Date:		2016_01_01			*/
/*	File:		GoFileTest.go			*/
/********************************************************/

package main

import (
	. "GoGlb"
	C "GoCls"
)

func main() {
	file := new(C.GoFile)
	name := "./GoFileTest.dat"
	data := "Copyright (C) 2016 Gong Li Bin		\n"

	/* file */
	GoDebug("Will GoFileOpen %s", name)
	if GO_SUCCESS == GoError(file.GoFileOpen(name, C.GO_FL_CRTE | C.GO_FL_APPD | C.GO_FL_RDWR)) {
		GoDebug("Will GoFileWrite %s", name)
		file.GoFileWrite([]byte(data))
		file.GoFileWrite([]byte(data))
		num, _ := file.GoFileWrite([]byte(data))
		if num > 0 {
			GoDebug("GoFileWrite %s %d", name, num)
		} else {
			GoDebug("GoFileWrite %s 0", name)
		}
		GoDebug("%s size: %d", name, file.GoFileSize())
		GoDebug("Will GoFileClose %s", name)
		file.GoFileClose()
	}

	buf := make([]byte, GO_KBYTES8)
	GoDebug("Will GoFileOpen %s", name)
	if GO_SUCCESS == GoError(file.GoFileOpen(name, C.GO_FL_RDOL)) {
		ret, err := file.GoFileRead(buf)
		if nil == err {
			GoPrint("Read: len[%d]\n%s", ret, string(buf))
		}
		GoDebug("Will GoFileClose %s\n", name)
		file.GoFileClose()
	}

	/* log */
	cnt := 0
	log := new(C.GoLog)
	GoDebug("Will GoLogOpen %s", name)
	if GO_SUCCESS == GoError(log.GoFileOpen(name, C.GO_FL_RDOL)) {
		log.GoLogBufio()
		for {
			cnt += 1
			line, info := log.GoLogLine('\n')
			if C.GO_FL_EOF != info {
				GoPrint("Read[%02d]: %s", cnt, line)
			} else {
				break
			}
		}
		GoDebug("Will GoFileClose %s", name)
		file.GoFileClose()
	}

	/* delete */
	GoDebug("Will GoFileDelete %s", name)
	file.GoFileDelete(name)
}
